﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HZP
{
    static class UTIL
    {
        public static void InitDGVColumns(DataGridView dgv, ColConfig[] cols)
        {
            dgv.EditMode = DataGridViewEditMode.EditOnEnter;
            dgv.AutoGenerateColumns = false;
            dgv.Columns.Clear();
            cols.ForEach<ColConfig>((col) =>
            {
                DataGridViewColumn c=new DataGridViewColumn();
                switch (col.ColType)
                {
                    case "string":
                        c = new DataGridViewTextBoxColumn();
                        break;
                    case "combo":
                        c = new DataGridViewComboBoxColumn();
                        (c as DataGridViewComboBoxColumn).DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
                        break;
                    default:
                        break;
                }
                c.Frozen = col.Freezeable;
                c.ReadOnly = !col.Editable;
                if (col.Editable)
                {
                    c.DefaultCellStyle.BackColor = Color.LightCoral;
                    //c.DefaultCellStyle.ForeColor = Color.Wheat;
                    c.DefaultCellStyle.Font = new Font("宋体", 10, FontStyle.Bold);
                }
                c.Name = col.FieldName;
                c.HeaderText = col.ColName;
                c.DataPropertyName = col.FieldName;
                c.Width = col.Width;
                dgv.Columns.Add(c);
            });
        }

        /// <summary>
        /// 给列绑定下拉,第一列是名称,第二列为值
        /// </summary>
        /// <param name="col">传入下拉的列</param>
        /// <param name="sql">第一列是名称,第二列为值</param>
        public static void BindComboColumn(DataGridViewComboBoxColumn col,DataTable dt)
        {
            col.DataSource =dt;
            col.DisplayMember = dt.Columns[0].ColumnName;
            col.ValueMember = dt.Columns[1].ColumnName;
        }
    }
}
